package  
{
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.net.SharedObject;
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		Keeps track of whole collection of achievements and stats related to them.  This is like session stats
	 * 	multiplied a gazillion complexity...
	 * 		Dangerous to have public variables but hopefully no one else breaks them! They're public so it's easier
	 * 	to copy to/from SharedObject... make sure AchievMenu doesn't touch any public variables!
	 * 
	 */
	public class Achievements extends EventDispatcher
	{
		private const ACHIEV_LIST_X_STARTLOC:int = 30;
		private const ACHIEV_LIST_Y_STARTLOC:int = 30;
		private const ACHIEV_LIST_NUM_COL:int = 5;				//number of achievments displayed in a row (in achievmenu)
		//private var ss:SessionStats;
		
		//basic statistics
		public var numRounds:int;								//current round number playing
		public var avgScore:Number;								//avg score of pirates left in a round
		public var avgNumRolls:Number;							//avg tries a round lasts
		public var avgRoundTime:Number;							//avg length of time it takes to complete a round
		public var lastScore:int;								//score from last round
		public var lastDiceRollSum:int;							//dice roll sum from last roll
		public var diceHistory:Vector.<Point>;					//history of dice rolls for this round
		public var achMoney:int;								//totalMoney from SessionStats, updated in CheckForAchievements()
		
		//count statistics (need to be public so my AchievementObject can see them)
		public var numRoundsWon:int;							//number of rounds where money was won
		public var numRolls:int;								//number of dicerolls in a round
		public var perfectCount:int;							//number of rounds won with score = 0
		public var clear1OnFirstRoll:int;						//number of rounds executed 1 on the first roll
		public var clear7OnFirstRoll:int;						//number of rounds executed 7 on the first roll
		public var clear9OnFirstRoll:int;						//number of rounds executed 9 on the first roll
		//public var achievsClear179:int;					//get above 3 achievements
		public var clearAtLeastOne:int;
		public var clearAtLeastTwo:int;
		public var clearAtLeastThree:int;						//clear 3 pirates in a single roll
		public var clearAtLeastFour:int;						//clear 4 pirates in a single roll
		public var numPiratesExecuted:int;						//number of pirates executed
		public var rollTwoInARow:int;							//number of times you've rolled twice in a row
		public var rollComboTwoInARow:int;						//# of times same combo has been rolled twice in a row
		public var rollComboThreeInARow:int;					//# of times same combo has been rolled three times in a row
		public var rollADouble:int;								//e.g. 1,1; 2,2; 3,3; etc...
		public var rollSnakeEyes:int;							//roll 1,1
		public var firstRollSnakeEyes:int;						//roll 1,1 on first roll
		public var firstRollDoubleSixes:int;					//roll 6,6 on first roll
		public var clear789in4rolls:int;						//# of times cleared 7,8,9 in exactly first 4 rolls
		public var clear789in3rolls:int;						//# of times cleared 7,8,9 in exactly first 3 rolls
		public var endGameScore1:int;							//end game with score of 1
		public var endGameScore42:int;							//end game with score of 42
		public var only1perRoll1Left:int;						//clear only one per roll with one left
		public var winIn8Rolls:int;								//almost same as above but won (AKA win in exactly 8 rolls)
		public var loseScoreLowTier:int;						//lose a round with a score of 17 or more
		public var loseScoreHiTier:int;							//lose a round with a score of 23 or more
		
		public var achievementList:Vector.<AchievementObject>;	//list of all achievements
		public var achievementsReceived:Vector.<AchievementObject>;	//list of achievements awarded/completed
		private var achievementDialogs:Vector.<MovieClip>;		//list of dialogs that user clicks in successive order
																//if multiple achievements achieved at one time
		private var achievementDialogImgData:ImageData;
		
		private var mainRef:Main;								//so we can addChild() the achievement dialog
		
		public var achRcvdIdx:Vector.<int>; //made this public so i could know the indexes of all the achievements -miles
		public var last3Ach:Vector.<int>; //a list of the last three achievements recieved
		
		public function Achievements(mR:Main) 
		{
			this.mainRef = mR;
			
			achRcvdIdx = new Vector.<int>;	//stores the index# of "received" list (for display in achievement button)
			
			//this.ss = ss;
			diceHistory = new Vector.<Point>;
			
			mR.addEventListener(AchievementEvent.EXECUTED_PLANK, Event_ExecutedPlank, true);
			mR.addEventListener(AchievementEvent.END_OF_ROUND, Event_EndOfRound, true);
			mR.addEventListener(AchievementEvent.DICE_ROLL, Event_RollDice, true);
			
			achievementDialogs = new Vector.<MovieClip>;
			achievementDialogImgData = new ImageData(0, 0, "achdlg", 0xFFFF80, 800, 480, 0.6)
			achievementList = new Vector.<AchievementObject>;		//total list
			achievementsReceived = new Vector.<AchievementObject>;	//completed list
			
			//achievementList.push(new AchievementObject(mR,_achievName, _achievDesc, _achievCheckVar, _achievNum));
			var _achl:AchievementsLoader = new AchievementsLoader(mR, this, achievementList);
			
			InitNewGame();
		}
		public function InitNewGame():void
		{
			avgScore = -1;
			avgNumRolls = -1;
			avgRoundTime = -1;
			numRounds = 1;			//start at round#1
			lastScore = -1;
			lastDiceRollSum = -1;
			
			numRoundsWon = 0;
			numRolls = 0;
			perfectCount = 0;
			clear1OnFirstRoll = 0;
			clear7OnFirstRoll = 0;
			clear9OnFirstRoll = 0;
			//achievsClear179 = 0;
			clearAtLeastOne = 0;	//test achievement
			clearAtLeastTwo = 0;	//test achievement
			clearAtLeastThree = 0;
			clearAtLeastFour = 0;
			numPiratesExecuted = 0;
			rollTwoInARow = 0;
			rollComboTwoInARow = 0;
			rollComboThreeInARow = 0;
			rollADouble = 0;
			rollSnakeEyes = 0;
			firstRollSnakeEyes = 0;
			firstRollDoubleSixes = 0;
			clear789in4rolls = 0;
			clear789in3rolls = 0;
			endGameScore1 = 0;
			endGameScore42 = 0;
			only1perRoll1Left = 0;
			winIn8Rolls = 0;
			loseScoreLowTier = 0;
			loseScoreHiTier = 0;
			
			//initialize achievements too
			for (var i:int = 0; i < achievementList.length; i++) {
				achievementList[i].ResetAchieved();
			}
			while (achievementsReceived.length > 0) {
				achievementsReceived.pop();
			}
			while (diceHistory.length > 0) {				//initialize dice history
				diceHistory.pop();
			}
		}
		
		//SaveYourself()
		//saved achievements list/rcvd in 2 loops to preserve the order in which they were received?
		//may be silly to have separate "list" and "received" vectors, but just makes it easier to load and save i guess
		//...initially it was b/c the "achievementsReceived" list might not have contained ALL achievements received and only 5? ohwell
		public function SaveYourself(data:Object):void
		{
			//var _achListInts:Vector.<int> = new Vector.<int>;	//stores the indexes of achievements that have been achieved
			achRcvdIdx = new Vector.<int>;
			var _achRcvdRound:Vector.<int> = new Vector.<int>;	//stores the above's round when achieved
			var _achRcvdWhen:Vector.<int> = new Vector.<int>;	//stores the above's time when achieved in ms
			var i:int;
			var j:int;
			//for (i = 0; i < achievementList.length; i++) {
				//if (achievementList[i].IsAchieved()) {			//search thru all the achievements and
					//_achListInts.push(i);						//only store the ones that have been achieved
					//_achListRound.push(achievementList[i].GetRoundAchieved());
					//_achListWhen.push(achievementList[i].GetWhenAchieved());
				//}
			//}
			//data.achievementListInts = _achListInts.slice();	//store a copy of it just in case it changes before u want to save it again
			//trace("data.achievementListInts.length=" + data.achievementListInts.length);
			
			//may be stupid way to do it, but i don't want to mess with achID#'s and it's only BigO(N^2)
			for (i = 0; i < achievementsReceived.length; i++) {			//search thru achievements received (i)
				for (j = 0; j < achievementList.length; j++) {
					if (achievementsReceived[i] == achievementList[j]) {//and store the achievementList "index"# (j) of the ones received
						achRcvdIdx.push(j);
						_achRcvdRound.push(achievementsReceived[i].GetRoundAchieved());	//or achievementList[j]'s is the same ach
						_achRcvdWhen.push(achievementsReceived[i].GetWhenAchieved());
					}
				}
			}
			data.achievementRcvdIdx = achRcvdIdx.slice();	//store a copy of it just in case it changes before u want to save it again
			trace("data.achievementRcvdIdx.length=" + data.achievementRcvdIdx.length);
			data.achievementRcvdRound = _achRcvdRound.slice();
			data.achievementRcvdWhen = _achRcvdWhen.slice();
			
			data.avgScore = avgScore;
			data.avgNumRolls = avgNumRolls;
			data.avgRoundTime = avgRoundTime;
			data.numRounds = numRounds;
			data.lastScore = lastScore;
			//data.lastDiceRoll = lastDiceRoll;
			
			data.numRoundsWon = numRoundsWon;
			data.numRolls = numRolls;
			data.perfectCount = perfectCount;
			data.clear1OnFirstRoll = clear1OnFirstRoll;
			data.clear7OnFirstRoll = clear7OnFirstRoll;
			data.clear9OnFirstRoll = clear9OnFirstRoll;
			//data.achievsClear179 = achievsClear179;
			data.clearAtLeastOne = clearAtLeastOne;	//test achievement
			data.clearAtLeastTwo = clearAtLeastTwo;	//test achievement
			data.clearAtLeastThree = clearAtLeastThree;
			data.clearAtLeastFour = clearAtLeastFour;
			data.numPiratesExecuted = numPiratesExecuted;
			data.rollTwoInARow = rollTwoInARow;
			data.rollComboTwoInARow = rollComboTwoInARow;
			data.rollComboThreeInARow = rollComboThreeInARow;
			data.rollADouble = rollADouble;
			data.rollSnakeEyes = rollSnakeEyes;
			data.firstRollSnakeEyes = firstRollSnakeEyes;
			data.firstRollDoubleSixes = firstRollDoubleSixes;
			data.clear789in4rolls = clear789in4rolls;
			data.clear789in3rolls = clear789in3rolls;
			data.endGameScore1 = endGameScore1;
			data.endGameScore42 = endGameScore42;
			data.only1perRoll1Left = only1perRoll1Left;
			data.winIn8Rolls = winIn8Rolls;
			data.loseScoreLowTier = loseScoreLowTier;
			data.loseScoreHiTier = loseScoreHiTier;
		}
		
		//LoadYourself()
		public function LoadYourself(data:Object):void
		{
			InitNewGame();				//make sure to do initializing game stuff before loading in the data
			var i:int;
			//for (i = 0; i < data.achievementListInts.length; i++) {
				//achievementList[data.achievementListInts[i]].SetAchieved();
			//}
			for (i = 0; i < data.achievementRcvdIdx.length; i++) {
				achievementsReceived.push(achievementList[data.achievementRcvdIdx[i]]);
				achievementList[data.achievementRcvdIdx[i]].SetAchieved(data.achievementRcvdRound[i], data.achievementRcvdWhen[i]);
			}
			
			avgScore = data.avgScore;
			avgNumRolls = data.avgNumRolls;
			avgRoundTime = data.avgRoundTime;
			numRounds = data.numRounds;
			lastScore = data.lastScore;
			//lastDiceRoll = data.lastDiceRoll;
			
			numRoundsWon = data.numRoundsWon;
			numRolls = data.numRolls;
			perfectCount = data.perfectCount;
			clear1OnFirstRoll = data.clear1OnFirstRoll;
			clear7OnFirstRoll = data.clear7OnFirstRoll;
			clear9OnFirstRoll = data.clear9OnFirstRoll;
			//achievsClear179 = data.achievsClear179;
			clearAtLeastOne = data.clearAtLeastOne;	//test achievement
			clearAtLeastTwo = data.clearAtLeastTwo;	//test achievement
			clearAtLeastThree = data.clearAtLeastThree;
			clearAtLeastFour = data.clearAtLeastFour;
			numPiratesExecuted = data.numPiratesExecuted;
			rollTwoInARow = data.rollTwoInARow;
			rollComboTwoInARow = data.rollComboTwoInARow;
			rollComboThreeInARow = data.rollComboThreeInARow;
			rollADouble = data.rollADouble;
			rollSnakeEyes = data.rollSnakeEyes;
			firstRollSnakeEyes = data.firstRollSnakeEyes;
			firstRollDoubleSixes = data.firstRollDoubleSixes;
			clear789in4rolls = data.clear789in4rolls;
			clear789in3rolls = data.clear789in3rolls;
			endGameScore1 = data.endGameScore1;
			endGameScore42 = data.endGameScore42;
			only1perRoll1Left = data.only1perRoll1Left;
			winIn8Rolls = data.winIn8Rolls;
			loseScoreLowTier = data.loseScoreLowTier;
			loseScoreHiTier = data.loseScoreHiTier;
		}
		
		private function formatData(dat:String):String
		{
			return "\t<dat round='" + (numRounds-1) + "' name='" + dat + "'>" + this[dat] + "</dat>\n";
		}
		
		//GetAchDataAsString()
		public function GetAchDataAsString():String
		{
			var i:int;
			var s:String = new String();
			s = "<data>\n";
			for (i = 0; i < achievementsReceived.length; i++) {			//search thru achievements received (i)
				s += "\t<achiev name='" + achievementsReceived[i].GetName() + "' roundAchieved='" + 
										achievementsReceived[i].GetRoundAchieved() + "' timeAchieved='" + 
										TimeFormat.formatTime(achievementsReceived[i].GetWhenAchieved()/1000) + "'></achiev>\n";
			}
			return s;
		}
		//GetDataAsString()
		//returns string of achievement data in xml format
		//for some reason...... data for money and gametime are in sessionstats instead of here
		public function GetDataAsString():String
		{
			var s:String = new String();
			var _gameLen:Number = mainRef.GetGameRef().GetSS().GetGameTime();
			
			//s += "<dat name='GameLengthInMilliseconds'>" + _gameLen + "</dat>\n";
			s += "\t<dat round='" + (numRounds-1) + "' name='GameTime'>" + TimeFormat.formatTime(_gameLen / 1000) + "</dat>\n";
			s += "\t<dat round='" + (numRounds-1) + "' name='totalMoney'>" + mainRef.GetGameRef().GetSS().GetTotalMoney() + "</dat>\n";
			//s += "\t<dat round='" + (numRounds-1) + "' name='dataLastTrackedTime'>" + mainRef.GetGameRef().GetSS().GetDataLastTrackedTime() + "</dat>\n";
			//s += formatData("numRounds"); //don't need this cuz it's already in every line
			s += formatData("avgScore");
			s += formatData("avgNumRolls");
			s += formatData("avgRoundTime");
			s += formatData("lastScore");
			s += formatData("numRoundsWon");
			s += formatData("numRolls");
			s += formatData("perfectCount");
			s += formatData("clear1OnFirstRoll");
			s += formatData("clear7OnFirstRoll");
			s += formatData("clear9OnFirstRoll");
			s += formatData("clearAtLeastTwo");
			s += formatData("clearAtLeastThree");
			s += formatData("clearAtLeastFour");
			s += formatData("numPiratesExecuted");
			s += formatData("rollComboTwoInARow");
			s += formatData("rollComboThreeInARow");
			s += formatData("rollADouble");
			s += formatData("rollSnakeEyes");
			s += formatData("firstRollSnakeEyes");
			s += formatData("firstRollDoubleSixes");
			s += formatData("clear789in4rolls");
			s += formatData("clear789in3rolls");
			s += formatData("endGameScore1");
			s += formatData("endGameScore42");
			s += formatData("only1perRoll1Left");
			s += formatData("winIn8Rolls");
			s += formatData("loseScoreLowTier");
			s += formatData("loseScoreHiTier");
			//s += "</data>";
			//trace(s);
			return s;
		}
		
		//GetAchievementList()
		public function GetAchievementList():Vector.<AchievementObject> {
			return achievementList;
		}
		//GetAchievementsReceived()
		public function GetAchievementsReceived():Vector.<AchievementObject> {
			return achievementsReceived;
		}
		//UpdateListAppearance()
		//Updates the appearance of the achievementList
		//their location, and "highlighting" the last 5 achieved?
		public function UpdateListAppearance():void
		{
			for (var i:int = 0; i < achievementList.length; i++)
			{
				//update appearance sets proper location, name, and changes anything else (like color?)
				achievementList[i].UpdateAppearance(ACHIEV_LIST_X_STARTLOC + 125 * (i % ACHIEV_LIST_NUM_COL), ACHIEV_LIST_Y_STARTLOC + 45 * Math.floor(i / ACHIEV_LIST_NUM_COL));
			}
		}
		public function UpdateReceivedAppearance():void
		{
			var _aLen:int = achievementsReceived.length;
			for (var i:int = _aLen - 1; (i >= 0) && (i >= (_aLen - 5)) ; i--)	//only displays the last 5 achievements received
			{
				achievementsReceived[i].UpdateAppearance(0, 45 * (((_aLen-1 - i)%5) + 1));
			}
		}
		//AchievedDialogInputMouseClick()
		//mouseclick will remove the achievement achieved dialog
		protected function AchievedDialogInputMouseClick():void
		{
			mainRef.removeChild(achievementDialogs[0]);
			achievementDialogs.shift();
			DisplayAchievementsInOrder();	//display the next achievement achieved if there is another
		}
		
		//CheckForAchievements()
		//call this every time an achievement MIGHT happen, to check them all...
		//i.e. after stats change for any reason
		private function CheckForAchievements():void
		{
			achMoney = mainRef.GetGameRef().GetSS().GetTotalMoney();	//need to record our own variable so .CheckIfAchieved() will find it here
			
			for each (var ach:AchievementObject in achievementList)
			{
				//trace("checking achievement: " + ach.GetName() + ":" + !ach.IsAchieved() + ":" + ach.CheckIfAchieved());
				if (!ach.IsAchieved() && ach.CheckIfAchieved())
				{
					//then we just got this achievement just now - pop up dialog! (well, only one at a time, so do it after "for" loop)
					var _gameLen:Number = mainRef.GetGameRef().GetSS().GetGameLengthInMilliseconds();
					ach.SetAchieved(numRounds, _gameLen);
					achievementsReceived.push(ach);
					//add it to the queue of achievements to display
					//achievementDialogs.push(new MenuButton(achievementDialogImgData, AchievedDialogInputMouseClick));
					achievementDialogs.push(new GenButton(0,0,NotificationOverlay,AchievedDialogInputMouseClick));
					//edit the achievement dialog's text
					achievementDialogs[achievementDialogs.length - 1].mc.header.text = "Achievement Achieved:";
					achievementDialogs[achievementDialogs.length - 1].mc.second.text = ach.GetName();
					achievementDialogs[achievementDialogs.length - 1].mc.desc.text =  "Click the icon on the set-up screen\n to learn more";
					achievementDialogs[achievementDialogs.length - 1].mc.bust_icon.visible = false;
					achievementDialogs[achievementDialogs.length - 1].mc.ach_icon.visible = true;
					achievementDialogs[achievementDialogs.length - 1].mc.ach_icon.gotoAndStop(ach.achievNum);
				
				}
			}
			DisplayAchievementsInOrder();	//display the first achievement if there is one
		}
		//checks to see if the achievement with that ID# is achieved
		public function IsIDAchieved(checkID:int):Boolean
		{
			for each (var ach:AchievementObject in achievementsReceived) {
				if (checkID == ach.GetID()) {
					return true;
				}
			}
			return false;
		}
		
		//DisplayAchievementsInOrder()
		//displays the next achievement in the queue
		//(if there's at least one: for use when multiple achievements achieved at once)
		private function DisplayAchievementsInOrder():void
		{
			if (achievementDialogs.length > 0) {
				mainRef.addChild(achievementDialogs[0]);
			}
		}
		
		//Event_ExecutedPlank
		//e.data is a vector<int> that holds the number for each pirate executed in that subround
		//cheats to get lineup values, since it didn't get passed in from e.data
		private function Event_ExecutedPlank(e:AchievementEvent):void
		{
			trace("Event: " + e.type + " : " + e.data);
			var i:int;
			if (numRolls == 1)
			{
				for each (i in e.data) {
					if (i == 1) {
						clear1OnFirstRoll++;					//ach: clear 1 on the first roll
					}
					if (i == 7) {
						clear7OnFirstRoll++;					//ach: clear 7 on the first roll
					}
					if (i == 9) {
						clear9OnFirstRoll++;					//ach: clear 9 on the first roll
					}
				}
			}
			//if (achievsClear179 == 0							//if we don't have it yet, check these 3
						//&& clear1OnFirstRoll > 0
						//&& clear7OnFirstRoll > 0
						//&& clear9OnFirstRoll > 0) {
				//achievsClear179 = 1;							//ach: got those 3 achievements
			//}
			if (e.data.length >= 1) { clearAtLeastOne++; }		//test achievements
			if (e.data.length >= 2) { clearAtLeastTwo++; }		//test achievements
			if (e.data.length == 3) { clearAtLeastThree++; }	//ach: clear exactly 3 pirates in one roll
			if (e.data.length == 4) { clearAtLeastFour++; }		//ach: clear exactly 4 pirates in one roll
			numPiratesExecuted += e.data.length;				//ach: number of pirates executed
			
			//is it cheating if i'm checking main for the lineup's values? ohohwell!
			var _lv:Vector.<int> = mainRef.GetGameRef().GetLineupValues();
			var _found78or9:Boolean = false;
			for each (i in _lv) {
				if (i == 7 || i == 8 || i == 9) {
					_found78or9 = true;
				}
			}
			
			if (!_found78or9) {
				if (numRolls == 4) {
					clear789in4rolls++;			//ach: there's no 7,8,9 on lineup (cleared them already) within the first 4 rolls
				}
				if (numRolls == 3) {
					clear789in3rolls++;			//ach: there's no 7,8,9 on lineup (cleared them already) within the first 3 rolls
				}
			}
			
			CheckForAchievements();
		}
		
		//Event_RollDice()
		//e.data is a vector<int> that holds values for each die (default = 2 die)
		private function Event_RollDice(e:AchievementEvent):void
		{
			trace("Event: " + e.type + " : " + e.data);
			var _currentDiceSum:int = e.data[0] + e.data[1];			//sum
			var _curDiceRoll:Point = new Point(e.data[0], e.data[1]);	//using Point to simulate dice
			
			if(diceHistory.length > 0) {								//check for 2 roll combinations same in a row
				if (diceHistory[0].equals(_curDiceRoll) || 
							(diceHistory[0].x == _curDiceRoll.y && diceHistory[0].y == _curDiceRoll.x)) {
					rollComboTwoInARow++;
					if (diceHistory.length > 1) {						//now check for 3 in a row
						if (diceHistory[1].equals(_curDiceRoll) || 
								(diceHistory[1].x == _curDiceRoll.y && diceHistory[1].y == _curDiceRoll.x)) {
							rollComboThreeInARow++;
						}
					}
				}
			}
			
			if (lastDiceRollSum == _currentDiceSum) {
				rollTwoInARow++;
			}
			numRolls++;									//incr # of rolls (started at 0, so do this at beginning when it happens)
			if (e.data[0] == e.data[1]) {
				rollADouble++;							//ach: rolled a double
				if (e.data[0] == 1) {
					rollSnakeEyes++;					//ach: rolled snake eyes
					if (numRolls == 1) {
						firstRollSnakeEyes++;			//ach: first roll got snake eyes
					}
				}
				if (numRolls == 1 && e.data[0] == 6) {
					firstRollDoubleSixes++;				//ach: first roll double sixes ("midnight")
				}
			}
			
			CheckForAchievements();
			
			lastDiceRollSum = _currentDiceSum;			//now update to record history
			diceHistory.unshift(_curDiceRoll);			//add dice to beginning of history
		}
		
		//Event_EndOfRound()
		//e.data is int that holds the round's score: gameRef.GetRoundScore()
		private function Event_EndOfRound(e:AchievementEvent):void
		{
			trace("Event: " + e.type + " : " + e.data);
			
			if (numRounds == 1) {				//if this is the first round, avg's need to be "initialized" with this round's data
				avgScore = e.data;
				avgNumRolls = numRolls;
				avgRoundTime = mainRef.GetGameRef().GetSS().GetRoundLengthInMilliseconds();
			} else {
				avgScore = ((avgScore * (numRounds-1)) + e.data) / (numRounds);
				avgNumRolls = ((avgNumRolls * (numRounds-1)) + numRolls) / (numRounds);
				avgRoundTime = ((avgRoundTime * (numRounds-1)) + mainRef.GetGameRef().GetSS().GetRoundLengthInMilliseconds()) / (numRounds);
			}
			if (e.data == 0) {
				perfectCount++;								//ach: perfect score!
			}
			if (e.data == 1) {
				endGameScore1++;							//ach: end game with score of 1
			}
			if (e.data == 42) {
				endGameScore42++;							//ach: end game with score of 42
			}
			if (numRolls == 8) {
				if (e.data == 0) {
					winIn8Rolls++;							//ach: win in 8 rolls
				} else if (e.data == 1) {
					only1perRoll1Left++;					//ach: clear only 1 per roll with 1 left (more strict than endGameScore1)
				}
			}
			if (mainRef.GetGameRef().GetSS().CalcWinningsWithBetAndScore() > 0) {
				numRoundsWon++;								//ach: money was won this round
			}
			if (e.data >= 17 && e.data <= 22) {
				loseScoreLowTier++;						//ach: lose a round with score of 17 to 22
			}
			if ( e.data >= 23) {
				loseScoreHiTier++;						//ach: lose round score 23 or more
			}
			
			CheckForAchievements();
			
			//now update/reset the end of round data
			numRounds++;
			lastDiceRollSum = -1;
			lastScore = e.data;								//record last score here
			numRolls = 0;
			while(diceHistory.length > 0) diceHistory.pop();//reset dice history
		}
		
		
	}

}